<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SrProject;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SrProjectController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SrProject(), function (Grid $grid) {
            $grid->disableEditButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableViewButton();

            $grid->column('name');
            $grid->column('baseurl')->label();
            $grid->column('create_user')->display(function ($v){
                return Administrator::find($v)->username;
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');

            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                //接口明细
                $url = 'detail/'. $actions->getKey();
                $actions->append('<a href="' . admin_url($url) .'" class="shop-relation-edit-btn"><span class="d-none d-sm-inline"><i class="feather icon-edit-1"></i>&nbsp;&nbsp;接口开发</span></a>');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SrProject(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('baseurl');
            $show->field('description');
            $show->field('create_user');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SrProject(), function (Form $form) {
            $form->text('name')->width(7, 3)->required();
            $form->text('baseurl')->width(7, 3)->required()->help('示例：/shop');
            $form->textarea('description')->width(7, 3);
            $form->hidden('create_user');
            $form->image('d_pric')->autoUpload()->url('images/upload');

            $form->saving(function (Form $form) {
                // 修改用户提交的数据
                $form->create_user = Admin::user()->id;
            });
        });
    }
}
